import { getCatalogAll, selectBookList } from '@/api'




function a(controller, tag) {
    if (!controller || controller.length == 0) {
        return null
    }
    let t
    for (let i = 0; i < controller.length; i++) {
        if (controller[i].name == tag) {
            return controller[i]
        } else {
            //向子根寻找
            t = a(controller[i].children, tag)
            if (t) {
                return t
            }
        }
    }
    return null
}

function pushAll(a, b) {
    if (!a || a.length == 0) {
        return
    }
    if (!b) {
        return
    }
    for (let i = 0; i < a.length; i++) {
        b.push(a[i])
    }
}
function b(controller) {
    if (!controller[0].children || controller[0].children.length == 0) {
        //最末尾
        return controller
    }
    let arr = []
    for (let i = 0; i < controller.length; i++) {
        pushAll(controller[i].children, arr)
    }
    return arr
}
function c(controller, tag) {
    let t = a(controller, tag)
    if (!t) {
        return null
    }
    if (!t.children || t.children.length == 0) {
        return [t]
    }
    return b(t.children)
}



export default {
    namespaced: true,
    actions: {
        getCatalog(context) {
            getCatalogAll().then(({ data }) => {
                context.commit('machiningCatalog', data.data)
            }).catch((err) => {

            });
        },
        //点击左侧数据
        selectBookListPage(context, value) {
            console.log("获取数据", value)
            let queryCondition = {
                catalogs: "",
                serialStatus: "",
                updateTime: "",
                startWords: "",
                finishWords: "",
                promotion: ""
            }
            let t = c(context.state.catalogAll, value.checkedCatalog)
            console.log("初步计算得到分类", t)
            let ca = []
            if (t) {
                t.forEach(element => {
                    ca.push(element.name)
                });
            }
            queryCondition.catalogs = ca;
            console.log("计算得到分类", ca)
            queryCondition.serialStatus = value.checkedSerialStatus

            queryCondition.updateTime = value.checkUpdateTime

            if (value.checkedWords == 30) {
                queryCondition.finishWords = 300000
            } else if (value.checkedWords == 50) {
                queryCondition.startWords = 300000
                queryCondition.finishWords = 500000
            } else if (value.checkedWords == 100) {
                queryCondition.startWords = 500000
                queryCondition.finishWords = 1000000
            } else if (value.checkedWords == 200) {
                queryCondition.startWords = 1000000
                queryCondition.finishWords = 2000000
            } else if (value.checkedWords > 200) {
                queryCondition.startWords = 2000000
            }

            // queryCondition.startWords = queryCondition.startWords * 10000

            if (value.checkedAttr) {
                queryCondition.promotion = -1
            }

            console.log("最终输入", queryCondition)
            console.log("查询开始2")
            selectBookList(queryCondition, 0, 20).then(({ data }) => {
                //queryCondition用于sort时使用
                context.commit('updateBookListPage', { data: data.data, queryCondition })
            }).catch((err) => {

            });

        },
        /**
         * 
         * @param {*} context 
         * @param {排序类型} type 
         * @param {当前页} startPage 
         * @param {每页显示数据} pageSize 
         */
        getBookPageBySortFiled(context, value) {
            console.log(value)

            console.log("查询条件", context.state.query)
            if (value.sortType) {
                context.state.query.sortType = value.sortType
            }
            let query = context.state.query

            //选择排序方式
            console.log("查询开始1")
            console.log(query)
            selectBookList(query, value.startPage, value.pageSize).then(({ data }) => {
                context.commit('updateBookListPage', { data: data.data, queryCondition: query })
            }).catch((err) => {

            });
        }

    },
    mutations: {
        machiningCatalog(state, data) {
            state.catalogAll = data
            data.forEach(item, index => {
                subordinateCatalog.push(item.children)
            });
        },
        updateBookListPage(state, value) {
            console.log("修改条件组合", value)
            state.bookListPage = value.data;
            if (value.queryCondition != null) {
                state.query = value.queryCondition;
            }
        }
    },
    state: {
        catalogAll: [{ name: "男生" }, { name: "男生" }],
        bookListPage: {},
        query: {},
    },
    getters: {

    }
}